using UnityEngine;
using System.Collections;

public class PlanetCamera : MonoBehaviour {
	
	public GameObject character;
	
    public Vector3 positionVector;
	public Vector3 lookVector;
	
    private SmoothFollower posFollow;
	private SmoothFollower lookFollow;
	//private Vector3 lastVelocityDir;
	private Vector3 lastPos;

    
    public Vector3 offSet = new Vector3(0.0f, 0.0f, -30.0f);
    Vector3 currentUp;

   private PhysicsCharacterMotor phsxMotor;
	
	void Start () {
		posFollow = new SmoothFollower(0.0f,0.0f);
		lookFollow = new SmoothFollower(0.0f,0.0f);
		posFollow.Update(transform.position,0,true);
		lookFollow.Update(character.transform.position,0,true);
		//lastVelocityDir = character.transform.forward;
		lastPos = character.transform.position;

        phsxMotor = GameObject.Find("Player").GetComponent<PhysicsCharacterMotor>();

	}
	
	void LateUpdate () {
		//lastVelocityDir += (character.transform.position-lastPos)*8;
		//lastPos = character.transform.position;
		//lastVelocityDir += character.transform.forward*Time.deltaTime;
		//lastVelocityDir = lastVelocityDir.normalized;
		
        //offset of camera and character
        /*Vector3 horizontal = transform.position-character.transform.position;
		
        Vector3 horizontal2 = horizontal;
		Vector3 vertical = character.transform.up;
		Vector3.OrthoNormalize(ref vertical,ref horizontal2);
		if (horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2;*/

        transform.up = character.transform.up;

        //offSet = Vector3.Cross(offSet, transform.position);

        currentUp = character.transform.up;

        offSet.x = currentUp.x * 6;

        offSet.y = currentUp.y * 6;

        //Debug.Log("currentUp = " + currentUp + " | offSet = " + offSet);

        transform.position = character.transform.position + offSet;

        //posFollow.Update(Vector3 targetPositionNew, float deltaTime)
        /*transform.position = posFollow.Update(
			character.transform.position + horizontal*Mathf.Abs(positionVector.z) + vertical*positionVector.y,
			Time.deltaTime*12
		);*/
		
		//horizontal = lastVelocityDir;
		//Vector3 look = lookFollow.Update(character.transform.position + horizontal*lookVector.z - vertical*lookVector.y, Time.deltaTime);

        //creates a cross product to stabilize the right vector on camera
        //Vector3 crossX = Vector3.Cross(transform.forward, -character.transform.up);
        //transform.right = crossX;
        
        
        //transform.rotation = Quaternion.FromToRotation(transform.forward, transform.position) * transform.rotation;
	}

}
